package com.ltx.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.ltx.admin.common.convention.result.Result;
import com.ltx.admin.common.convention.result.Results;
import com.ltx.admin.dto.req.UserLoginReqDTO;
import com.ltx.admin.dto.req.UserRegisterReqDTO;
import com.ltx.admin.dto.req.UserUpdateReqDTO;
import com.ltx.admin.dto.resp.UserActualRespDTO;
import com.ltx.admin.dto.resp.UserLoginRespDTO;
import com.ltx.admin.dto.resp.UserRespDTO;
import com.ltx.admin.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/*
 * @description 用户控制层
 * @author 罗添煦
 * @create 2024-10-13 下午8:26
 */
@RestController
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    /**
     * 根据用户名查询用户
     */
    @GetMapping("/api/short-link/v1/user/{username}")
    public Result<UserRespDTO> getUserByUserName(@PathVariable("username") String username) {
        UserRespDTO result = userService.getUserByUserName(username);
        return Results.success(result);
    }

    /**
     * 根据用户名查询无脱敏用户信息
     */
    @GetMapping("/api/short-link/v1/actual/user/{username}")
    public Result<UserActualRespDTO> getUserActualByUserName(@PathVariable("username") String username) {
        UserRespDTO result = userService.getUserByUserName(username);
        return Results.success(BeanUtil.toBean(result, UserActualRespDTO.class));
    }

    /**
     * 判断用户名是否存在
     */
    @GetMapping("/api/short-link/v1/user/has-username")
    public Result<Boolean> hasUserName(@RequestParam("username") String username) {
        return Results.success(userService.hasUserName(username));
    }

    /**
     * 注册用户
     */
    @PostMapping("/api/short-link/v1/user/register")
    public Result<Void> register(@RequestBody UserRegisterReqDTO userRegisterReqDTO) {
        userService.register(userRegisterReqDTO);
        return Results.success();
    }

    /**
     * 修改用户
     */
    @PutMapping("/api/short-link/v1/user/update")
    public Result<Void> update(@RequestBody UserUpdateReqDTO userUpdateReqDTO) {
        userService.update(userUpdateReqDTO);
        return Results.success();
    }

    /**
     * 用户登录
     */
    @PostMapping("/api/short-link/v1/user/login")
    public Result<UserLoginRespDTO> login(@RequestBody UserLoginReqDTO userLoginReqDTO) {
        return Results.success(userService.login(userLoginReqDTO));
    }

    /**
     * 检查是否登录 True表示已登录,False表示未登录
     */
    @GetMapping("/api/short-link/v1/user/check-login")
    public Result<Boolean> checkLogin(@RequestParam(value = "username") String username, @RequestParam(value = "token") String token){
        return Results.success(userService.checkLogin(username,token));
    }

    /**
     * 用户退出登录
     */
    @DeleteMapping("/api/short-link/v1/user/logout")
    public Result<Void> logout(@RequestParam(value = "username") String username, @RequestParam(value = "token") String token){
        userService.logout(username,token);
        return Results.success();
    }
}
